package cn.itcast.job;

import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import cn.itcast.zookeeper.annotation.ElasticDataFlowJob;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
// @Component
@ElasticDataFlowJob(name = "orderTimeoutJob",
        cron = "0/10 * * * * ?",
        overwrite = true,
        shardingTotalCount = 3,
        streamingProcess = false)
public class OrderTimeoutJob implements DataflowJob<Order> {
    
    @Autowired
    private OrderService orderService;
    
    //基于分片项，每次抓取100个超时订单。
    //0号-------->ID % 3 = 0
    //1号-------->ID % 3 = 1
    //2 号------->Id % 3 = 2
    
    @Override
    public List<Order> fetchData(ShardingContext shardingContext) {
        //1.获取分片项
        int item = shardingContext.getShardingItem();
        
        //2.获取总分片项
        int totalCount = shardingContext.getShardingTotalCount();
        
        //3.基于分片项和总分片项，每次从订单表中抓取20个数据（超时订单）。
        List<Order> orderList = orderService.findOrderTimeOut(item, totalCount);
        return orderList;
    }
    
    @Override
    public void processData(ShardingContext shardingContext, List<Order> list) {
        log.info(shardingContext.getShardingItem() + "分片项," + list.size());
        for (Order order : list) {
            orderService.updateStatus(order, "4");
        }
    }
}
